﻿using Domain.Flow;
using MediatR;
using U.B.ErrorCode;
using U.B.ReadApi.Command.Raddit.BW;
using U.B.ReadApi.Dto;
using U.B.Repistory;

namespace U.B.ReadApi.CommandHander.Raddit.BWHA
{
    public class GetproductSSCommandHander : IRequestHandler<GetproductSSCommand, ApiResult<ProductDto>>
    {
        private readonly IRepistory<Product> _productRepository;
        private readonly IRepistory<ProductClass> ProductClassrepistory;
        public GetproductSSCommandHander(IRepistory<Product> productRepository, IRepistory<ProductClass> productClassrepistory)
        {
            _productRepository = productRepository;
            ProductClassrepistory = productClassrepistory;
        }
        /// <summary>
        /// 详情页面页首
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<ProductDto>> Handle(GetproductSSCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ProductDto> apiResult = new ApiResult<ProductDto>();
            var result = (from a in _productRepository.GetAll()
                          join b in ProductClassrepistory.GetAll() on a.ProductClassId equals b.ProductClassId
                          select new ProductDto
                          {
                              ProductName = b.ProductName,
                              BatchId = a.BatchId,
                              FirmName = a.FirmName,
                              Full = a.Full,
                              KeepSafe = a.KeepSafe,
                              Permission = a.Permission,
                              ProductId = a.ProductId,
                              CreationDate = a.CreationDate,
                              Creator = a.Creator,
                              Images = a.Images,
                          }).Where(x => x.BatchId == request.BatchId).FirstOrDefault();
            apiResult.code = Renum.显示成功;
            apiResult.Message = Renum.显示成功.ToString();
            apiResult.Data = result;
            return Task.FromResult(apiResult);
        }
    }
}
